package vip.xumy.admin.um.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import vip.xumy.admin.um.mapper.IUserMapper;
import vip.xumy.admin.um.pojo.User;
import vip.xumy.admin.um.service.IUserService;
import vip.xumy.admin.utils.LoginUtil;
import vip.xumy.core.exception.CoreException;
import vip.xumy.core.golbal.GlobalConstant;
import vip.xumy.core.utils.DateUtil;
import vip.xumy.core.utils.StringUtil;

/**	Ownership belongs to the company
 *	author:mengyxu
 *	date:2019年5月10日
 */

@Service
public class UserService implements IUserService {

	@Autowired
	private IUserMapper userMapper;

	@Override
	public List<User> queryAll() {
		List<User> list = userMapper.findUser(new User());
		return list;
	}

	@Override
	public User findUser(String userId, String key) {
		User user = getById(userId);
		if(user != null && StringUtil.equals(user.getKey(), key)) {
			return user;
		}
		return null;
	}
	
	public User getById(String userId) {
		if(StringUtil.isEmpty(userId)){
			return null;
		}
		User exp = new User();
		exp.setUserId(userId);
		List<User> list = userMapper.findUser(exp);
		if(list == null || list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	private User getByPhone(String phone) {
		if(StringUtil.isEmpty(phone)){
			return null;
		}
		User exp = new User();
		exp.setPhone(phone);
		List<User> list = userMapper.findUser(exp);
		if(list == null || list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	private User getByidNum(String idCard) {
		if(StringUtil.isEmpty(idCard)){
			return null;
		}
		User exp = new User();
		exp.setIdCard(idCard);
		List<User> list = userMapper.findUser(exp);
		if(list == null || list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<User> queryByExample(User example) {
		return userMapper.queryByExample(example);
	}

	@Override
	@Transactional
	public void insert(User user) throws CoreException {
		//判断用户名是否重复
		String userId = user.getUserId();
		if(getById(userId) != null){
			throw new CoreException("用户名："+userId+"已经存在");
		}
		String idCard = user.getIdCard();
		if(getByidNum(idCard) != null){
			throw new CoreException("身份证号码："+idCard+"已绑定其他用户");
		}
		String phone = user.getPhone();
		if(getByPhone(phone) != null){
			throw new CoreException("手机号码："+phone+"已绑定其他用户");
		}
		//设置默认值后插入数据库
		user.setKey(DigestUtils.md5DigestAsHex(user.getKey().getBytes()));
		user.setStatus("0");
		userMapper.insert(user);
		userMapper.deleteUserRole(userId);
		if(user.getRoles() != null && !user.getRoles().isEmpty()) {
			userMapper.insertUserRole(user);
		}
	}

	@Override
	@Transactional
	public void updateInfo(User user) throws CoreException {
		String phone = user.getPhone();
		User old = getByPhone(phone);
		if(old != null && !StringUtil.equals(user.getUserId(), old.getUserId())){
			throw new CoreException("手机号码："+phone+"已绑定其他用户");
		}
		String idCard = user.getIdCard();
		old = getByidNum(idCard);
		if(old != null && !StringUtil.equals(user.getUserId(), old.getUserId())){
			throw new CoreException("身份证号码："+idCard+"已绑定其他用户");
		}
		user.setKey(null);
		userMapper.update(user);
		userMapper.deleteUserRole(user.getUserId());
		if(user.getRoles() != null && !user.getRoles().isEmpty()) {
			userMapper.insertUserRole(user);
		}
	}

	@Override
	@Transactional
	public void deleteById(String userId) {
		//删除用户
		userMapper.delete(userId);
		userMapper.deleteUserRole(userId);
		LoginUtil.remveLoginInfo(userId);
	}

	@Override
	public void updateLogin(String userId) {
		User user = new User();
		user.setUserId(userId);
		user.setLastLogin(DateUtil.format(new Date(), DateUtil.FORMAT19_LINE_YYYYMMDDHHMMSS));
		userMapper.update(user);
	}


	@Override
	@Transactional
	public void updateKey(User user) throws CoreException {
		User old = getById(user.getUserId());
		if(user != null && StringUtil.equals(user.getKey(), old.getKey())) {
			if("0".equals(old.getStatus())) {
				user.setStatus("1");
			}
			userMapper.update(user);
			LoginUtil.remveLoginInfo(user.getUserId());
		}else {
			throw new CoreException("修改密码失败,用户名或密码错误");
		}
	}

	@Override
	public void resetKey(String userId) {
		User user = getById(userId);
		user.setNewKey(GlobalConstant.DEFAULT_PASSWORD);
		user.setStatus("0");
		userMapper.update(user);
	}
	
}
